// pages/mine/second_page/discount.js
var App = getApp();
var page = 0;
var hasData = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allColumn: ['未使用', '已使用', '已失效'],
    currentTab: 0,
    discountList:[],
    useDiscountList: [],
    invalidList: [],
    contentDis: false//缺省页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    page = 0;
    hasData = true;
    this.getDiscountList(this.data.currentTab)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (hasData) {
      page+=10;
      this.getDiscountList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //back
  backLastPage: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  // 导航切换
  swichNavigator: function (e) {
    wx.showLoading({
      title: '加载中',
    })
    var index = e.currentTarget.dataset.current;
    this.setData({
      currentTab: index,
      discountList: []
    })
    page = 0;
    hasData = true;
    this.getDiscountList(index);
  },
  // 删除
  onDelete: function (e) {
    let id = e.currentTarget.dataset.id;
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/coupon/user/del',
      data: {
        token: wx.getStorageSync('token'),
        id: id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if(res.data.error == 0){
          wx.showToast({
            title: '删除成功'
          })
          that.onLoad();
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:800
          })
        }
      }
    })
  },
  // 优惠券详情页
  showDetailMsg:function(e){
    var item = e.currentTarget.dataset.index;
    let arr = 'discountList[' + item +'].show';
    this.setData({
      [arr]: !this.data.discountList[item].show
    })
  },
  //获取优惠券列表
  getDiscountList:function(){
    let that = this;
    let max = 10;
    wx.request({
      url: App.data.globalAPI + '/coupon/user/getlist',
      data: {
        type: that.data.currentTab,
        start:page,
        num:max,
        userId: wx.getStorageSync('userInfo').uid,
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          let data = res.data.data.items;
          let originList = that.data.discountList;
          if (data.length > 0 || originList.length > 0) {
            originList.push(...data);
            that.setData({
              contentDis: false,
              discountList: originList
            })
            if (data.length < max && !that.data.contentDis) {
              hasData = false;
            }
          } else {
            that.setData({
              contentDis: true
            })
          }
          wx.hideLoading();
        }
      }
    })
  }
})
